<?php
namespace hc;
use hc\session;
/*
 * 验证码
 * 允许大小设定
 * session 名称 verification
 */
class yzm{
	//宽 默认100
	static private $width=100;
	//高 默认30
	static private $height=30;
	//背景设置
	public static $bgcolor = false;
	//设置尺寸入口
	public static function set_size($width,$height){
		self::$width=$width;
		self::$height=$height;
	}
	//生成验证码
	public static function run(){
		//生成图片功能声明
		header("cache-control:no-cache,must-revalidate");
		//图片格式声明
		header("Content-Type:image/png");
		//header("Content-Disposition:attachment;filename=aa.png");
		$im=imagecreate(self::$width,self::$height) or W_ERROR('图片功能障碍!');
		//随机生成背景
		if(self::$bgcolor==true){
			$background_color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		}
		//字体颜色
		$text_color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		//线条颜色
		$line_color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		//绘制矩形
		imagefilledrectangle($im,0,0,self::$width,self::$height,$line_color);
		//像素点颜色
		$pixel_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		//绘制像素点
		for($i=0;$i<200;$i++){
			ImageSetPixel($im, rand(0,self::$width),rand(0,self::$height),$pixel_color);
		}
		/*$string = "abcdefghijklmnopqrstuvwxyz0123456789";
		$code = "";
		for($i=0;$i<4;$i++){
			$code = $code.$string[rand(0,35)];
		}
		$_SESSION['Code'] = $code;
		imagestring($im,5,10,9,$code,$text_color);
		*/
		//设置验证码session
		$ascii_number="";
		$yzm_str = "";
		//产生随机数
		for($i=0;$i<4;$i++)
		{
			$number=rand(0,2);
			switch($number)
			{
				case 0:$rand_number=rand(48,57);break;//数字
				case 1:$rand_number=rand(65,90);break;//大写
				case 2:$rand_number=rand(97,122);break;//小写
			}
			$ascii=sprintf("%c",$rand_number);
			$ascii_number=$ascii_number.$ascii;
			$yzm_str.=$ascii." ";
		}
		//设置验证码session值
		$_SESSION['yzm_code'] = md5(strtolower($ascii_number));
		//设置验证码session值
		//设置文字到验证码
		imagestring($im,5,10,9,$yzm_str,$text_color);
		//显示图片
		imagepng($im);
		//摧毁图片
		imagedestroy($im);
	}
}
